//导入重置样式
require("../../assets/css/reset.css");
//导入公共样式
require("../../assets/css/basic.css");
//导入标准样式
require("../../assets/css/normalize.css");
//导入iconfont.css
require("../../assets/fonts/iconfont.css");
//导入swiper插件
require("../../libs/swiper/swiper-bundle.min.css");
//导入tabbar公共底部导航
require("../../assets/css/tabbar.less");
//导入样式
require("./index.less");
//引入swiperjs
const Swiper = require("../../libs/swiper/swiper-bundle.min.js");
//引入js模块
const dom = require("../../utils/dom.js");
//引入axios
const axios = require("axios");
document.addEventListener("DOMContentLoaded", function () {
	//渲染tabbar
	dom.tabbar("index");
	//点击课程训练跳转页面
	dom.get(".main3").addEventListener("click", function () {
		window.location.href = "sports_course.html";
	});
	//点击户外跑步跳转页面
	dom.get(".main4").addEventListener("click", function () {
		window.location.href = "sports_run.html";
	});
	//轮播图
	var mySwiper = new Swiper(".swiper", {
		// direction: 'vertical', // 垂直切换选项
		loop: true, // 循环模式选项
		autoplay: true, //自动播放

		// 如果需要分页器
		pagination: {
			el: ".swiper-pagination",
			clickable: true,
		},
	});

	//发请求获取用户个人信息
	axios.defaults.baseURL = "http://fitness.h5.itsource.cn:3701";
	let token = localStorage.getItem("token");
	axios.defaults.headers.common["Authorization"] = "Bearer " + token;
	axios.get("/api/user/info").then((res) => {
		let data = res.data.data;
		console.log(data);
		//渲染排名
		dom.get("#rankNum").textContent = data.ranking;
		//渲染徽章
		dom.get("#badgeNum").textContent = data.badges;
	});
	//发送请求获取打卡信息
	function renderClock() {
		axios.get("/api/user/clockInInfo").then((res) => {
			//渲染打卡天数
			dom.get("#clockNum").textContent = res.data.data.clockCount;
			let btn = dom.get("#clockBtn");
			if (res.data.data.isClockIn) {
				//已打卡
				btn.disabled = true; //禁用
				btn.style.backgroundColor = "#999";
				btn.textContent = "已打卡";
			}
		});
	}
	renderClock();

	//绑定打卡的事件
	dom.get("#clockBtn").addEventListener("click", function () {
		//发请求打卡
		axios.post("/api/user/clockIn").then((res) => {
			if (res.data.errno === 0) {
				alert("打卡成功");
				renderClock();
			} else {
				alert("已打卡");
			}
		});
	});
});
